package com.huangliusong.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
public class TimerInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.err.println("afterCompletion");
		long start = (long) request.getAttribute("startTim");
		System.err.println("postHandle");
		System.out.println("耗时：" + (new Date().getTime() - start));
		System.err.println("ex is " + arg3);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler_base,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		long start = (long) request.getAttribute("startTim");
		System.err.println("postHandle");
		System.out.println("耗时：" + (new Date().getTime() - start));

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		// TODO Auto-generated method stub
		System.err.println("preHandle");
		System.out.println(((HandlerMethod) object).getBean().getClass().getName());
		System.out.println(((HandlerMethod) object).getMethod().getName());
		request.setAttribute("startTim", new Date().getTime());
		return true;
	}

}
